package com.gravata.enigma.model;

import java.io.Serializable;
import java.util.regex.Pattern;

import android.util.Log;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName="filtro")
public class Filtro extends EntidadeAbstrata {
	private static final long serialVersionUID = -7545630817254228784L;

	public static final String REGRA_FIELD="regra_id";
	public static final String SATELITE_FIELD="satelite_id";
	public static final String TRANSPONDER_FIELD="transponder_id";

	@DatabaseField(generatedId=true)
	int id;

	@DatabaseField(canBeNull=false,columnName="filtro")
	private String filtro;

	@DatabaseField(canBeNull=true,columnName="negado",dataType=DataType.BOOLEAN)
	private boolean negado;

	@DatabaseField(canBeNull=true,columnName="inicio",dataType=DataType.BOOLEAN)
	private boolean inicio;

	@DatabaseField(canBeNull=true,columnName="fim",dataType=DataType.BOOLEAN)
	private boolean fim;

	@DatabaseField(canBeNull=true,columnName="completa",dataType=DataType.BOOLEAN)
	private boolean completa;

	@DatabaseField(canBeNull=true,columnName="hd",dataType=DataType.BOOLEAN)
	private boolean hd;

	@DatabaseField(canBeNull=false,foreign = true, columnName = REGRA_FIELD,foreignAutoRefresh=true)
	private Regra regra;

	@DatabaseField(canBeNull=true, foreign = true, columnName = SATELITE_FIELD,foreignAutoRefresh=true)
	private Satelite satelite;

	@DatabaseField(canBeNull=true,foreign = true, columnName = TRANSPONDER_FIELD,foreignAutoRefresh=true)
	private Transponder transponder;

	public Serializable getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}


	public boolean isNegado() {
		return negado;
	}

	public void setNegado(boolean negado) {
		this.negado = negado;
	}


	public String getFiltro() {
		return filtro;
	}

	public void setFiltro(String filtro) {
		this.filtro = filtro;
	}

	public Regra getRegra() {
		return regra;
	}

	public void setRegra(Regra regra) {
		this.regra = regra;
	}

	public void setInicio(boolean inicio) {
		this.inicio = inicio;
	}
	public boolean isInicio() {
		return inicio;
	}
	public void setFim(boolean fim) {
		this.fim = fim;
	}
	public boolean isFim() {
		return fim;
	}

	public boolean isCompleta() {
		return completa;
	}

	public void setCompleta(boolean completa) {
		this.completa = completa;
	}

	public boolean isHd() {
		return hd;
	}
	public void setHd(boolean hd) {
		this.hd = hd;
	}

	public Satelite getSatelite() {
		return satelite;
	}
	public void setSatelite(Satelite satelite) {
		this.satelite = satelite;
	}
	public Transponder getTransponder() {
		return transponder;
	}
	public void setTransponder(Transponder transponder) {
		this.transponder = transponder;
	}

	public boolean atende(String teste){
		Pattern pattern=getRegex();
		Log.d("ATENDE",pattern.pattern()+" - "+teste+" = "+ Boolean.toString(pattern.matcher(teste).find()));
		return pattern.matcher(teste).find();
	}

	private Pattern getRegex(){
		String reg="";
		if(!filtro.equals("*")){
			reg="("+filtro+")";
			if(!isCompleta() && !isInicio() && !isFim()){
				reg=".*"+reg+".*";
			}
			if(isInicio())
				reg="^"+reg;

			if(isFim())
				reg+="$";
		}else{
			reg=".*";
		}
		Pattern regex=Pattern.compile(reg,Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
		return regex;
	}

	public String getPattern(){
		return getRegex().pattern();
	}

	public Filtro() {
	}

}